/**
 * Created by DoveXL on 2018/12/9.
 */

$(document).ready(function () {
    queryTeam(null);
});
function queryTeam(TeamName) {
    $.ajax({
        async: true,
        type: "post",
        url: "/VolunteerPlatform/TeamInfo/CertainTeamInfo",
        dataType: "json",
        contentType : 'application/json;charset=utf-8',
        data: JSON.stringify({"page":1,"pagesize":8,"TeamName":TeamName}),
        cache: false,
        success: function(data) {
            var result = eval(data);
            $("#TeamManagementInfo tr").empty();
            for (var i = 0; i<result.pagelist.length; i++) {
                var row = CreateTeamRow(result.pagelist[i]);
                $("#TeamManagementInfo").append(row);
            };
            var currentPage = result.currentpage; //当前页数
            var pageCount = result.pagecount; //总页数
            var options = {
                bootstrapMajorVersion: 3, //版本
                currentPage: currentPage, //当前页数
                totalPages: pageCount, //总页数
                numberOfPages: 8,
                shouldShowPage:true,//是否显示该按钮
                itemTexts: function (type, page, current) {
                    switch (type) {
                        case "first":
                            return "首页";
                        case "prev":
                            return "上一页";
                        case "next":
                            return "下一页";
                        case "last":
                            return "末页";
                        case "page":
                            return page;
                    }
                },
                onPageClicked: function (event, originalEvent, type, page) {
                    $.ajax({
                        async: true,
                        url: "/VolunteerPlatform/TeamInfo/CertainTeamInfo",
                        type: "post",
                        dataType : "json",
                        contentType : 'application/json;charset=utf-8',
                        data: JSON.stringify({"page":page,"pagesize":8,"TeamName":TeamName}),
                        cache: false,
                        success: function (data) {
                            $("#TeamManagementInfo tr").empty();
                            var result = eval(data);
                            for (var i = 0; i <result.pagelist.length; i++) {
                                var row = CreateTeamRow(result.pagelist[i]);
                                $("#TeamManagementInfo").append(row);
                            };
                        }
                    });
                }
            };
            $('#TeampageList').bootstrapPaginator(options);
        }
    });
}
function CreateTeamRow(UserInfo) {
    var row = document.createElement('tr');
    var ChooseCell = document.createElement('td');
    var btnLabel = document.createElement('label');
    var btnCheckbox = document.createElement('input');
    btnCheckbox.setAttribute("type","checkbox");
    btnCheckbox.setAttribute("name","TeamCheckbox");
    btnLabel.appendChild(btnCheckbox);
    ChooseCell.appendChild(btnLabel);
    row.appendChild(ChooseCell);
    var UserNameCell = document.createElement('td');
    UserNameCell.innerHTML = UserInfo.user_teamname;
    row.appendChild(UserNameCell);
    var UserNumberCell = document.createElement('td');
    UserNumberCell.innerHTML = UserInfo.user_teamnumber;
    row.appendChild(UserNumberCell);
    var UserLeadingCell = document.createElement('td');
    UserLeadingCell.innerHTML = UserInfo.user_teamleading;
    row.appendChild(UserLeadingCell);
    var UserPhoneNumCell = document.createElement('td');
    UserPhoneNumCell.innerHTML = UserInfo.user_leadingphonenum;
    row.appendChild(UserPhoneNumCell);
    return row;
}
function ShowAddTeamInfoModal() {
    $("#AddTeamInfoName").val("");
    $("#AddTeamLeading").val("");
    $("#AddTeamLeadingPhonenum").val("");
    $("#AddTeamInfoFlag").html("");
    $("#BtnAddTeamInfo").removeAttr("disabled");
    $("#BtnAddTeamInfo").removeClass("disabled");
    $("#AddTeamInfoModal").modal();
}



function AddTeamInfoFunction() {
    var TeamName = $("#AddTeamInfoName").val().trim();
    var TeamLeading = $("#AddTeamLeading").val().trim();
    var TeamLeadingPhonenum=$("#AddTeamLeadingPhonenum").val().trim();
    if(checkChinese("团队名称",TeamName))
    {
        $.ajax({
            async: false,
            type: "post",
            url: "/VolunteerPlatform/TeamInfo/CheckTeamName",
            contentType: 'application/json;charset=utf-8',
            data: JSON.stringify({"TeamName": TeamName}),
            cache: false,
            success: function (data) {
                var result =data;
                if (result =="true")
                {
                    if(TeamLeading.length==0)
                    {
                        TeamLeading="无";
                    }
                    if(checkChinese("负责人",TeamLeading))
                    {
                        if(TeamLeadingPhonenum.length==0)
                        {
                            TeamLeadingPhonenum="无";
                           AddTeamInfo(TeamName,TeamLeading,TeamLeadingPhonenum)
                        }else if(TeamLeadingPhonenum.length!=0&&checkPhone(TeamLeadingPhonenum)==true)
                        {
                            AddTeamInfo(TeamName,TeamLeading,TeamLeadingPhonenum)
                        }else
                        {

                        }
                    }else
                    {
                        return false;
                    }
                }else if(result=="false")
                {
                    $("#AddTeamInfoFlag").html("单位名称已经存在!");
                    return false;
                }else if(result=="False")
                {
                    $("#AddTeamInfoFlag").html("增加时出现未知错误!");
                    return false;
                }else {
                    $("#AddTeamInfoFlag").html("返回时出现未知错误!");
                    return false;
                }
            }
        })
    }
}


function AddTeamInfo(TeamName,TeamLeading,TeamLeadingPhonenum) {
    $.ajax({
        async: false,
        type: "post",
        url: "/VolunteerPlatform/TeamInfo/AddTeamInfo",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify({
            "TeamName": TeamName,
            "TeamLeading": TeamLeading,
            "TeamLeadingPhonenum": TeamLeadingPhonenum
        }),
        cache: false,
        success: function (data) {
            if (data == "true") {
                $("#AddTeamInfoFlag").html("增加成功");
                $("#BtnAddTeamInfo").attr("disabled","disabled");
                $("#BtnAddTeamInfo").addClass("disabled");
            } else {
                $("#AddTeamInfoFlag").html("增加失败");
            }
        }
    })
}

function ShowChangeTeamInfoModal() {
    var count = $("input[name = 'TeamCheckbox']:checked").length;
    if(count==0)
    {
        alert("未选中任何行");
    }else if(count>1)
    {
        alert("选中超过一行!");
    }else
    {
        var CheckedNode = $("input[name = 'TeamCheckbox']:checked").parent().parent();
        var OriginTeamName = $(CheckedNode).next().html().trim();
        var TeamLeading = $(CheckedNode).next().next().next().html().trim();
        var TeamLeadingPhoneNum= $(CheckedNode).next().next().next().next().html().trim();
        if(TeamLeading=="无")
        {
            TeamLeading="";
        }
        if(TeamLeadingPhoneNum=="无")
        {
            TeamLeadingPhoneNum="";
        }
        $("#OriginTeamInfoName").html(OriginTeamName);
        $("#NewTeamInfoName").val(OriginTeamName);
        $("#ChangeTeamLeading").val(TeamLeading);
        $("#ChangeTeamLeadingPhonenum").val(TeamLeadingPhoneNum);
        $("#ChangeTeamInfoFlag").html("");
        $("#BtnChangeTeamInfo").removeAttr("disabled");
        $("#BtnChangeTeamInfo").removeClass("disabled");
        $("#ChangeTeamInfoModal").modal();
    }
}


function ChangeTeamInfoFunction() {
    var CheckedNode = $("input[name = 'TeamCheckbox']:checked").parent().parent();
    var OriginTeamNameTest= $(CheckedNode).next().html().trim();
    var TeamLeadingTest = $(CheckedNode).next().next().next().html().trim();
    var TeamLeadingPhoneNumTest= $(CheckedNode).next().next().next().next().html().trim();
    if(TeamLeadingTest=="无")
    {
        TeamLeadingTest="";
    }
    if(TeamLeadingPhoneNumTest=="无")
    {
        TeamLeadingPhoneNumTest="";
    }
    var OriginTeamName = $("#OriginTeamInfoName").html().trim();
    var NewTeamName = $("#NewTeamInfoName").val().trim();
    var TeamLeading = $("#ChangeTeamLeading").val().trim();
    var TeamLeadingPhonenum=$("#ChangeTeamLeadingPhonenum").val().trim();
    if(NewTeamName==OriginTeamNameTest&&TeamLeading==TeamLeadingTest&&TeamLeadingPhoneNumTest==TeamLeadingPhonenum)
    {
        $("#ChangeTeamInfoFlag").html("未修改任何信息，不能提交!");
        return false;
    }
    else {

        if (checkChinese("团队名称", NewTeamName)) {
            $.ajax({
                async: false,
                type: "post",
                url: "/VolunteerPlatform/TeamInfo/CheckTeamName",
                contentType: 'application/json;charset=utf-8',
                data: JSON.stringify({"TeamName": NewTeamName}),
                cache: false,
                success: function (data) {
                    var result = data;
                    if (result == "true" && OriginTeamName != NewTeamName) {
                        if (TeamLeading.length == 0) {
                            TeamLeading = "无";
                        }
                        if (checkChinese("负责人", TeamLeading)) {
                            if (TeamLeadingPhonenum.length == 0) {
                                TeamLeadingPhonenum = "无";
                                ChangeTeamInfo(OriginTeamName, NewTeamName, TeamLeading, TeamLeadingPhonenum);
                            } else if (TeamLeadingPhonenum.length != 0 && checkPhone(TeamLeadingPhonenum) == true) {
                                ChangeTeamInfo(OriginTeamName, NewTeamName, TeamLeading, TeamLeadingPhonenum);
                            } else {

                            }
                        } else {
                            return false;
                        }
                    } else if (result == "false" && OriginTeamName == NewTeamName) {
                        if (TeamLeading.length == 0) {
                            TeamLeading = "无";
                        }
                        if (checkChinese("负责人", TeamLeading)) {
                            if (TeamLeadingPhonenum.length == 0) {
                                TeamLeadingPhonenum = "无";
                                ChangeTeamInfo(OriginTeamName, NewTeamName, TeamLeading, TeamLeadingPhonenum);

                            } else if (TeamLeadingPhonenum.length != 0 && checkPhone(TeamLeadingPhonenum) == true) {
                                ChangeTeamInfo(OriginTeamName, NewTeamName, TeamLeading, TeamLeadingPhonenum);
                            } else {

                            }
                        } else {
                            return false;
                        }
                    } else if (result == "false" && OriginTeamName != NewTeamName) {
                        $("#ChangeTeamInfoFlag").html("单位名称已经存在!");
                        return false;
                    } else if (result == "False") {
                        $("#ChangeTeamInfoFlag").html("增加时出现未知错误!");
                        return false;
                    } else {
                        $("#ChangeTeamInfoFlag").html("返回时出现未知错误!");
                        return false;
                    }
                }
            })
        }
    }
}

function ChangeTeamInfo(OriginTeamName,NewTeamName,TeamLeading,TeamLeadingPhonenum) {
    $.ajax({
        async: false,
        type: "post",
        url: "/VolunteerPlatform/TeamInfo/ChangeTeamInfo",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify({
            "OriginTeamName":OriginTeamName,
            "NewTeamName": NewTeamName,
            "TeamLeading": TeamLeading,
            "TeamLeadingPhonenum": TeamLeadingPhonenum
        }),
        cache: false,
        success: function (data) {
            if (data == "true1") {
                $("#ChangeTeamInfoFlag").html("修改成功");
                var CheckedNode = $("input[name = 'TeamCheckbox']:checked").parent().parent();
                $(CheckedNode).next().html(NewTeamName);
                $(CheckedNode).next().next().next().html(TeamLeading);
                $(CheckedNode).next().next().next().next().html(TeamLeadingPhonenum);
                $("#BtnChangeTeamInfo").attr("disabled","disabled");
                $("#BtnChangeTeamInfo").addClass("disabled");
            } else {
                $("#ChangeTeamInfoFlag").html("修改失败");
            }
        }
    })
}

function ShowDeleteTeamInfoModal() {
    var count = $("input[name = 'TeamCheckbox']:checked").length;
    if(count==0)
    {
        alert("未选中任何行");
    }else if(count>1)
    {
        alert("选中超过一行!");
    }else
    {
        var CheckedNode = $("input[name = 'TeamCheckbox']:checked").parent().parent();
        var OriginTeamName = $(CheckedNode).next().html().trim();
        var TeamNumber = $(CheckedNode).next().next().html().trim();
        var TeamLeading = $(CheckedNode).next().next().next().html().trim();
        var TeamLeadingPhoneNum= $(CheckedNode).next().next().next().next().html().trim();
        $("#DeleteOriginTeamInfoName").html(OriginTeamName);
        $("#DeleteTeamNumber").html(TeamNumber);
        $("#DeleteTeamLeading").html(TeamLeading);
        $("#DeleteTeamLeadingPhonenum").html(TeamLeadingPhoneNum);
        $("#DeleteTeamInfoFlag").html("");
        $("#BtnDeleteTeamInfo").removeAttr("disabled");
        $("#BtnDeleteTeamInfo").removeClass("disabled");
        $("#DeleteTeamInfoModal").modal();
    }
}
function DeleteTeamInfoFunction() {
    var CheckedNode = $("input[name = 'TeamCheckbox']:checked").parent().parent();
    var OriginTeamName = $(CheckedNode).next().html().trim();
    DeleteTeamInfo(OriginTeamName);
}


function DeleteTeamInfo(TeamName) {
    $.ajax({
        async: false,
        type: "post",
        url: "/VolunteerPlatform/TeamInfo/DeleteTeamInfo",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify({
            "TeamName":TeamName
        }),
        cache: false,
        success: function (data) {
            if (data == "true2") {
                $("#DeleteTeamInfoFlag").html("删除成功");
                queryTeam(null);
                $("#BtnDeleteTeamInfo").attr("disabled","disabled");
                $("#BtnDeleteTeamInfo").addClass("disabled");
            } else {
                $("#DeleteTeamInfoFlag").html("删除失败");
            }
        }
    })
}


function ShowImportTeamInfoModal() {
    $("#batchFile").val("");
    $("#fileName").html("");
    $("#ImportTeamInfoFlag").html("");
    $("#ErrorTeamInfoExcel").css("display","none");
    $("#ImportTeamInfoModal").modal();
}


$(function() {

    $("#batchUploadBtn").attr('disabled', true);
    $("#batchUploadBtn").click(function () {
        $("#progressBar").width("0%");
        $("#BtnImportTeamInfo").attr('disabled', "disabled");
        $("#BtnImportTeamInfo").addClass('disabled');
        $(this).attr('disabled', true);
        $("#progressBar").parent().show();
        $("#progressBar").parent().addClass("active");
        UpladFile();
    })

    $("#batchFile").change(function () {
        $("#batchUploadBtn").val("上传");
        $("#fileName").html("");
        $("#ImportTeamInfoFlag").html("");
        $("#ErrorTeamInfoExcel").css("display","none");
        $("#progressBar").width("0%");
        var file = $(this).prop('files');
        if (file.length != 0) {
            $("#batchUploadBtn").attr('disabled', false);
        }

    });

    function UpladFile() {
        var fileObj = $("#batchFile").get(0).files[0]; // js 获取文件对象
        console.info("上传的文件：" + fileObj);
        var FileController = "/VolunteerPlatform/UploadFile/TeamInfoUpload"; // 接收上传文件的后台地址
        var form = new FormData();
        form.append("file", fileObj); // 文件对象
        var progressBar = $("#progressBar");
        progressBar.width("100%");
        $("#batchUploadBtn").val("正在上传.......");
        $.ajax({
            type: "POST",
            url: FileController,
            data: form,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function (data) {
                if(data=="UploadCompleted") {
                    $("#ImportTeamInfoFlag").html("上传完成");
                    $("#batchUploadBtn").attr('disabled', false);
                    $("#batchUploadBtn").val("上传");
                    $("#fileName").html(fileObj.name);
                    $("#progressBar").parent().removeClass("active");
                    $("#progressBar").parent().hide();
                    $("#BtnImportTeamInfo").removeAttr('disabled');
                    $("#BtnImportTeamInfo").removeClass('disabled');
                }else if(data=="UploadException")
                {
                    $("#ImportTeamInfoFlag").html("上传失败");
                    $("#batchUploadBtn").attr('disabled', false);
                    $("#batchUploadBtn").val("上传");
                }else
                {
                    $("#ImportTeamInfoFlag").html("其他未知错误");
                    $("#batchUploadBtn").attr('disabled', false);
                    $("#batchUploadBtn").val("上传");
                }
            },
            error: function (data) {
                $("#ImportTeamInfoFlag").html(data);
            }
        });
        return false;
    }
})

function DisplayDownload() {

    $("#ExportTeamInfoExcel").css("display","none");
}




function ImportExcelContent() {
    var FileName = $("#fileName").html().trim();
    $.ajax({
        async: false,
        type: "post",
        url: "/VolunteerPlatform/TeamInfo/ImportTeamInfo",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify({
            "FileName":FileName
        }),
        cache: false,
        success: function (data) {
            if(data=="0")
            {
                $("#ImportTeamInfoFlag").html("文件为空");
                $("#BtnImportTeamInfo").attr('disabled');
                $("#BtnImportTeamInfo").addClass('disabled');
                queryTeam(null);

            }else if (data == "1") {
                $("#ImportTeamInfoFlag").html("全部导入成功");
                $("#BtnImportTeamInfo").attr('disabled');
                $("#BtnImportTeamInfo").addClass('disabled');
                queryTeam(null);

            } else if(data=="2"){
                $("#ImportTeamInfoFlag").html("部分导入成功");

                $("#BtnImportTeamInfo").attr('disabled');
                $("#BtnImportTeamInfo").addClass('disabled');
                $("#ErrorTeamInfoExcel").css("display","block");
                queryTeam(null);

            }else if(data=="3")
            {
                $("#ImportTeamInfoFlag").html("导入失败");

            }else if(data =="4")
            {
                $("#ImportTeamInfoFlag").html("其他问题导致导入失败");

            }else if(data =="5")
            {
                $("#BtnImportTeamInfo").attr('disabled');
                $("#BtnImportTeamInfo").addClass('disabled');
                $("#ImportTeamInfoFlag").html("文件中没有可正确导入的记录");
                $("#ErrorTeamInfoExcel").css("display","block");
            }
            else
            {
                $("#ImportTeamInfoFlag").html("未知问题导致导入失败");
            }
        }
    })
}






function ShowExportTeamInfoModal() {
    $("#ExportTeamInfoExcel").css("display","none");
    $("#ExportTeamInfoModal").modal();
}
function ExportExcelContent()
{
    var checklist=[];
    checklist.push("Number");
    $("input[name = 'ExportField']:checked").each(function () {
        checklist.push($(this).val());
        }
    );
    if(checklist.length==0)
    {
        alert("任何一个字段都未选中!");
        return false;
    }
    $.ajax({
        async: false,
        type: "post",
        url: "/VolunteerPlatform/TeamInfo/ExportTeamInfo",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify({
            "checkFieldName": checklist
        }),
        cache: false,
        success: function (data) {
            if(data=="1")
            {
                $("#ExportTeamInfoExcel").css("display","block");
            }else
            {
            }
        }
    });
}









